<?php
/*
  Plugin Name: Usuários
  Version: 1.5
  Plugin URI: http://www.ricardolanes.com.br
  Description:
  Author: Ricardo Lanes
  Author URI: http://www.ricardolanes.com.br
 */

function usuarios_function_init() {
	global $wpdb;

	if ($_GET['id'] && isset($_GET['status'])) {
		$id = $_GET['id'];
		$status = ($_GET['status'] == '1') ? '-1' : '1';
		$usuarios = $wpdb->get_results("UPDATE wp_cliente set status = '$status' where id = '$id'");
	}

	if ($_POST['s']) {
		$s = $_POST['s'];
		$where = "WHERE nome LIKE '%$s%' OR cpf = '$s' OR email = '$s'";
	}

	$usuarios = $wpdb->get_results("SELECT * FROM wp_cliente $where ORDER BY registro desc");
	?>



	<div class="wrap">
		<div id="icon-edit" class="icon32 icon32-posts-post">
			<br>
		</div>
		<h2>Lista de Usuários</h2>


		<ul class="subsubsub">
			<li class="all">Total <span class="count">(<?= count($usuarios); ?>)</span></li>
		</ul>
		<form id="" action="?post_type=produto&page=loja_usuarios" method="POST">

			<p class="search-box">
				<label class="screen-reader-text" for="post-search-input">Pesquisar usuário:</label>
				<input type="search" id="post-search-input" name="s" value="" style='width: 250px;'>
				<input type="submit" name="" id="" class="button" value="Pesquisar clientes">
			</p>

		</form>			
		<table class="wp-list-table widefat fixed posts" cellspacing="0">
			<thead>
				<tr>
					<th scope="col" id="author" class="manage-column column-author" style="">Nome</th>
					<th scope="col" id="categories" class="manage-column column-categories" style="">Email</th>
					<th scope="col" id="date" class="manage-column column-date sortable asc" style="">Data</th>
				</tr>
			</thead>

			<tfoot>
				<tr>
					<th scope="col" id="author" class="manage-column column-author" style="">Nome</th>
					<th scope="col" id="categories" class="manage-column column-categories" style="">Email</th>
					<th scope="col" id="date" class="manage-column column-date sortable asc" style="">Data</th>
				</tr>
			</tfoot>


			<tbody id="the-list">
				<?
				foreach ($usuarios as $usuario) {

					$part = explode(" ", $usuario->registro);
					$data = implode('/', array_reverse(explode('-', $part[0]))) . " " . $part[1];
					?>

					<tr id="post-106" class="post-106 type-post status-publish format-standard hentry category-energetico alternate iedit author-other" valign="top">
						<td class="post-title page-title column-title">
							<strong style="<? if ($usuario->status == '-1' || $usuario->status == '0') { echo 'color:#990000;'; } else { echo 'color:#009900;'; } ?>">
								<?= $usuario->nome; ?>								
							</strong>
							<div class="row-actions">
								<span class="edit">
									<a href="javascript:jQuery('#inline_<?= $usuario->id; ?>').toggle();" title="Dados do Usuário">Ver Dados</a> | 
								</span>
								
								<span class="trash">
									<?
									if ($usuario->status == '1') {
										?>
										<a class="submitsuspend" title="Suspender Cliente" href="?post_type=produto&page=loja_usuarios&status=<?= $usuario->status; ?>&id=<?= $usuario->id; ?>">Desativar</a>
										<?
									} else {
										?>
										<a class="submitsuspend" title="Suspender Cliente" href="?post_type=produto&page=loja_usuarios&status=<?= $usuario->status; ?>&id=<?= $usuario->id; ?>">Ativar</a>
										<?
									}
									?>
									 | 
								</span>
								
								<span class="edit">
									<a href="javascript:confirm('Deseja realmente enviar nova senha?');" title="Enviar nova senha">Enviar Nova Senha</a>
								</span>
							</div>
							<div class="hidden" id="inline_<?= $usuario->id; ?>">
								<div class="post_title">Telefone: <?= $usuario->telefone; ?></div>
								<div class="post_title">CPF: <?= $usuario->cpf; ?></div>
								<div class="post_title">Email: <?= $usuario->email; ?></div>
								<div class="post_title">Senha: <?= $usuario->senha; ?></div>
								<div class="post_title">CEP: <?= $usuario->cep; ?></div>
								<div class="post_title">Endereço: <?= $usuario->endereco; ?></div>
								<div class="post_title">Numero: <?= $usuario->numero; ?></div>
								<div class="post_title">Complemento: <?= $usuario->complemento; ?></div>
								<div class="post_title">Bairro: <?= $usuario->bairro; ?></div>
								<div class="post_title">Cidade: <?= $usuario->cidade; ?></div>
								<div class="post_title">Estado: <?= $usuario->estado; ?></div>
								<div class="post_title">&nbsp;</div>
							</div>
						</td> 
						<td class="categories column-categories">
							<?= $usuario->email; ?>
						</td>
						<td class="date column-date">
							<abbr title="<?= $data; ?>"><?= $data; ?></abbr>
						</td> 
					</tr>

					<?
				}
				?>

			</tbody>
		</table>
	</div>

	<?
}

